package com.example.shiro.controller;

import com.example.shiro.service.LoginService;
import com.example.shiro.domain.Role;
import com.example.shiro.domain.User;
import com.example.shiro.dto.AddRoleDto;
import com.example.shiro.dto.AddUserDto;
import com.example.shiro.response.ResponseMsg;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @ Author     ：jujh
 * @ Date       ：Created in 9:23 2018/10/30
 * @ Description：
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    //退出的时候是get请求，主要是用于退出
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "login1";
    }

    //post登录
    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    public String login(String username, String password, HttpServletRequest request) {
        System.out.println("====进入登录controller====");
        ResponseMsg msg = new ResponseMsg();
        msg.setStatus(ResponseMsg.STATUS_SUCCESS);
        return "redirect:/index";
        //查询用户信息
//        User user = loginService.findByNameAndPassword(username, password);
//        if (user != null) {
//            request.getSession().setAttribute("userinfo", user);
//            System.out.println(request.getSession().getMaxInactiveInterval());
//            msg.setStatus(ResponseMsg.STATUS_SUCCESS);
//            return msg;
//        } else {
//            msg.setStatus(ResponseMsg.STATUS_FAILED);
//            msg.setMsg("用户名或密码错误！");
//            return msg;
//        }

    }

    @RequestMapping(value = "/index")
    public String index() {
        return "index";
    }

    //登出
    @RequestMapping(value = "/logout")
    public String logout() {
        return "logout";
    }

    //错误页面展示
    @RequestMapping(value = "/error", method = RequestMethod.POST)
    public String error() {
        return "error";
    }

    //数据初始化
    @RequestMapping(value = "/addUser")
    @ResponseBody
    public String addUser(AddUserDto userDto) {
        User user = loginService.addUser(userDto);
        return "addUser is ok! \n" + user;
    }

    //角色初始化
    @RequestMapping(value = "/addRole")
    @ResponseBody
    public String addRole(AddRoleDto addRoleDto) {
        Role role = loginService.addRole(addRoleDto);
        return "addRole is ok! \n" + role;
    }

    //注解的使用
    @RequiresRoles("admin")
    @RequiresPermissions("create")
    @RequestMapping(value = "/create")
    @ResponseBody
    public String create() {
        return "Create success!";
    }


    @RequestMapping(value = "/ajaxLogin")
    @ResponseBody
    public ResponseMsg doLogin(String userName, String password) {
        ResponseMsg msg = new ResponseMsg();
        msg.setStatus(ResponseMsg.STATUS_SUCCESS);
        return msg;
    }

}
